package wt.hpad.dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import wt.hpad.R;

/**
 * 简单对话框
 * Created by wt on 1.8.
 */

public class SimpleDialog {

    protected Context mContext;
    protected Activity mActivity;
    private int mRootViewid;
    private int mBtnConfirmViewid;
    private int mBtnCancelViewid;
    private TextView bt_cancel;
    private TextView bt_submit;

    private SimpleDialogHandler mHandler;
    private AlertDialog mDialog;
    private View mRootView;

    public SimpleDialog(Context mContext, Activity mActivity, int mRootViewid, int mBtnConfirmViewid, int mBtnCancelViewid) {
        this.mContext = mContext;
        this.mActivity = mActivity;
        this.mRootViewid = mRootViewid;
        this.mBtnConfirmViewid = mBtnConfirmViewid;
        this.mBtnCancelViewid = mBtnCancelViewid;

        buildDialog();
    }

    public SimpleDialog(Activity mActivity, int rootViewId) {
        this.mContext = mActivity.getApplicationContext();
        this.mActivity = mActivity;
        this.mRootViewid = rootViewId;
        this.mBtnConfirmViewid = R.id.btn_dialog_confirm;
        this.mBtnCancelViewid = R.id.btn_dialog_cancel;

        buildDialog();
    }

    public SimpleDialog(Activity mActivity) {
        this.mContext = mActivity.getApplicationContext();
        this.mActivity = mActivity;
        this.mRootViewid = R.layout.dialog_simple;
        this.mBtnConfirmViewid = R.id.btn_dialog_confirm;
        this.mBtnCancelViewid = R.id.btn_dialog_cancel;

        buildDialog();
    }

    private void buildDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
        mDialog = builder.create();

        mRootView = LayoutInflater.from(mContext).inflate(mRootViewid, null);
        //让对话框显示一个自己定义的对话框界面效果
        mDialog.setView(mRootView);
    }

    public void show() {
        mDialog.show();
    }

    public void setTitle(String title) {
        mDialog.setTitle(title);
    }

    public void setContent(String content) {
        TextView tv_content = (TextView) mRootView.findViewById(R.id.tv_content);
        tv_content.setText(content);
    }

    public void setHandler(SimpleDialogHandler mHandler) {
        this.mHandler = mHandler;
        initData();
    }

    public View getRootView() {
        return mRootView;
    }

    public void endDialog() {
        mDialog.dismiss();
    }

    private void initData() {
        if (mBtnConfirmViewid != 0) {
            bt_submit = (TextView) mRootView.findViewById(mBtnConfirmViewid);
            bt_submit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mHandler.onConfirm();
                    mDialog.dismiss();
                }
            });
        }
        if (mBtnCancelViewid != 0) {
            bt_cancel = (TextView) mRootView.findViewById(mBtnCancelViewid);
            bt_cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mHandler.onCancel();
                    mDialog.dismiss();
                }
            });
        }
    }


}
